package arr;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * ClassName: Demo1
 * Package: arr
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-10-23
 * Time: 22:43
 */

public class Demo1 {
    public static void main1(String[] args) {

        //冒泡排序
        int[] arr = {7, 9, 2, 4, 8, 3, 6, 1, 5};

        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = tmp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }

    //冒泡排序 优化手段
    public static void main(String[] args) {

        int[] arr = {7, 9, 2, 4, 8, 3, 6, 1, 5};

        for (int i = 0; i < arr.length - 1; i++) {
            boolean flag = true;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                //大于就交换
                if (arr[j] > arr[j+1]){
                    int tmp = arr[j];
                    arr[j] = arr[j +1];
                    arr[j+1] = tmp;

                    flag = false;
                }
            }
            if (flag){
                break;
            }
        }
        System.out.println(Arrays.toString(arr));
    }

}
